package IceAssembly;

import java.io.*;
import java.util.*;

/**
 *
 * <p><b>Original Author:</b>  Peter Dietz
 */
public class Misc {
    
    /**
    * Compares two files to find out if they are identical.
    * <p>
    * <b>Change Log:</b><br>
    * 04/26/08 - Thought up need for operation - Peter Dietz<br>
    * 04/27/08 - Coded the operation - Yogesh Jindal<br>
    * 04/27/08 - Documented the operation - Peter Dietz<br>
    * <p>
    * <b>Error Conditions Tested:</b><br>
    * INTERNAL: File I/O Errors, such as file doesn't exist<br>
    * <p>
    * <b>Date of module being installed:</b> 04/27/08
    * <p><b>Original Author:</b>  Yogesh Jindal
    * @param file1 The first file to be compared
    * @param file2 The second file to be compared
    * @return "1" if the files were identical, "0" if the files were different
    */
    public String CompareFiles (String file1, String file2) throws java.io.IOException{
        String file1_string="",file2_string="";

        //Reading the contents of the files
        BufferedReader br_file1 = new BufferedReader (new FileReader (file1));
        BufferedReader br_file2 = new BufferedReader (new FileReader (file2));
        String temp="";
        while((temp=br_file1.readLine())!=null){
            file1_string+=temp;
        }
        temp="";
        while((temp=br_file2.readLine())!=null){
            file2_string+=temp;
        }
        if(file1_string.contentEquals(file2_string)){
            return "1";
        }
        return "0";
    }
    
    /**
    * Compares two files to find out if they are identical.
    * <p>
    * <b>Change Log:</b><br>
    * 04/26/08 - Thought up need for operation - Peter Dietz<br>
    * 04/27/08 - Coded the operation - Yogesh Jindal<br>
    * 04/27/08 - Documented the operation - Peter Dietz<br>
    * <p>
    * <b>Error Conditions Tested:</b><br>
    * INTERNAL: File I/O Errors, such as file doesn't exist<br>
    * <p>
    * <b>Date of module being installed:</b> 04/27/08
    * <p><b>Original Author:</b>  Yogesh Jindal
    * @param file1 The first file to be compared
    * @param file2 The second file to be compared
    * @return "1" if the files were identical, "0" if the files were different
    */
    public String CompareLinkerFiles (String file1, String file2) throws java.io.IOException{
        String file1_string="",file2_string="";

        //Reading the contents of the files
        BufferedReader br_file1 = new BufferedReader (new FileReader (file1));
        BufferedReader br_file2 = new BufferedReader (new FileReader (file2));
        String temp="";
        while((temp=br_file1.readLine())!=null){
            if(!temp.startsWith("LH")){
                file1_string+=temp;
            }
        }
        temp="";
        while((temp=br_file2.readLine())!=null){
            if(!temp.startsWith("LH")){
                file2_string+=temp;
            }
        }
        if(file1_string.contentEquals(file2_string)){
            return "1";
        }
        return "0";
    }
    //Rather expensive operation
    
    /**
    * Resets all global variables to their initial state so that the program can 
    * run without having interference from previous runs. This is only neccessary
    * if you are running multiple instances of the program at once, such as during
    * testing. Normal operation of the assembler provided only a single input 
    * source program will not to be reset.
    * <p>
    * <b>Change Log:</b><br>
    * 04/28/08 - Initial Code, needed a way to set program to initial state - Peter Dietz
    * <p>
    * <b>Error Conditions Tested:</b><br>
    * none yet<br>
    * <p>
    * <b>Date of module being installed:</b> 04/28/08
    * <p><b>Original Author:</b>  Peter Dietz
    */
    public void ResetAssembler (){
        Control control_instance = new Control();
        Misc misc_instance = new Misc();
        Table table_instance = new Table();
        
        //Rest the program to be fresh
        table_instance.intermediate_row = 0;
        table_instance.start_index = 0;
        table_instance.error_row = 0;
        table_instance.symbol_row = 0;
        control_instance.location_counter = 0;
        control_instance.line_num = 0;
        control_instance.debug_status = "OFF";
        
        //Clear Error
        for(int i=0; i<3072; i++){
            for(int j=0; j<2; j++){
                table_instance.error_table[i][j] = "";
                }
            }
            //Clear Intermediate
            for(int i=0; i<1023; i++){
                for(int j=0; j<8; j++){
                    table_instance.intermediate_file[i][j] = "";
                }
            }
            //Clear Symbol
            for(int i=0; i<50; i++){
                for(int j=0; j<4; j++){
                    table_instance.symbol_table[i][j] = "";
                }
            }
        }
    }